#include "sms.h"

TextBox *textbox_new(GooCanvasItem *parent, gdouble cavas_x, gdouble cavas_y, gdouble cavas_w, gdouble cavas_h, gdouble text_w,
		                                    int bg_rgb, int font_rgb, Theme *theme) {
	TextBox *textbox = NULL;
	textbox = (TextBox *)g_malloc0(sizeof(TextBox) + 1);
	textbox->parent = parent;
	textbox->cavas_x = cavas_x;
	textbox->cavas_y = cavas_y;
	textbox->cavas_w = cavas_w;
	textbox->cavas_h = cavas_h;
	textbox->text_w = text_w;

	textbox->background = NULL;
	if(bg_rgb == -1) {

		textbox->bg_rgb = 0xffffff;
	} else {
		textbox->bg_rgb = bg_rgb;
	}

	if (font_rgb == -1) {
		textbox->font_rgb = 0;
	} else {
		textbox->font_rgb = font_rgb;
	}

	/*scrolled window*/
	textbox->scrolled_win = gtk_scrolled_window_new (NULL, NULL);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(textbox->scrolled_win), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
	gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (textbox->scrolled_win), GTK_SHADOW_IN);
	gtk_widget_show (textbox->scrolled_win);

	/*vadjustment*/
	textbox->vadjustment = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (textbox->scrolled_win));

	/*paste the scroll win to the goo Group*/
	textbox->goo_scrolled_win = goo_canvas_widget_new(textbox->parent, textbox->scrolled_win,
			                                          textbox->cavas_x, textbox->cavas_y, -1, -1, NULL);

	/*generate the canvas and take it to the scroll win*/
	textbox->scrolled_canvas = goo_canvas_new ();
    g_object_set (G_OBJECT (textbox->scrolled_canvas), "automatic-bounds", TRUE,
    		                         "bounds-from-origin", FALSE,
    		                         "bounds-padding", 4.0,
    		                         "background-color-rgb", textbox->bg_rgb,
    		                         "has-tooltip", TRUE, NULL);
    gtk_widget_set_size_request (textbox->scrolled_canvas, textbox->cavas_w, textbox->cavas_h);
    gtk_widget_show (textbox->scrolled_canvas);
    textbox->scrolled_canvas_root = goo_canvas_get_root_item (GOO_CANVAS (textbox->scrolled_canvas));
    gtk_container_add (GTK_CONTAINER (textbox->scrolled_win), textbox->scrolled_canvas);

	textbox->text = goo_canvas_text_new(textbox->scrolled_canvas_root, "", 0, 0, textbox->text_w, GTK_ANCHOR_NW,
			                            "font", "Sans 20",//theme->font[THEME_FONT_SMALL],
			                            "fill-color", textbox->font_rgb,
			                            "ellipsize", PANGO_ELLIPSIZE_NONE,
			                            "alignment", PANGO_ALIGN_LEFT,
			                            "wrap", PANGO_WRAP_WORD, NULL);
	//textbox->vadjustment_lower = 0.0;
	//textbox->vadjustment_upper = 0.0;

    return textbox;
}

gboolean textbox_hide(TextBox *textbox) {
	if (!textbox)
		return FALSE;

	gtk_widget_hide_all(textbox->scrolled_win);
	gtk_widget_hide_all(textbox->scrolled_canvas);
	return TRUE;
}

gboolean textbox_show(TextBox *textbox) {
	if (!textbox)
		return FALSE;

	gtk_widget_show_all(textbox->scrolled_win);
	gtk_widget_show_all(textbox->scrolled_canvas);
	return TRUE;
}

gboolean textbox_set_text(TextBox *textbox, gchar *text) {
	if (!textbox)
		return FALSE;
	if (!textbox->text) {
		return FALSE;
	}

	printf("start func %s\n" ,__FUNCTION__);

//	g_object_set(textbox->text, "text", "", NULL);
	g_object_set(textbox->text, "text", text, NULL);
	//gtk_adjustment_get_value(test_layout->vadjustment)
	//g_object_get(G_OBJECT(textbox->vadjustment), "lower", &(textbox->vadjustment_lower), NULL);
	//g_object_get(G_OBJECT(textbox->vadjustment), "upper", &(textbox->vadjustment_upper), NULL);
	//printf("lower: %f, upper: %f \n", textbox->vadjustment_lower, textbox->vadjustment_upper);

	return TRUE;
}

gboolean textbox_set_background_pixbuf(TextBox *textbox, GdkPixbuf *bg) {
	if (!textbox)
		return FALSE;
	if (textbox->background) {
		goo_canvas_item_remove(textbox->background);
		textbox->background = NULL;
	}
	textbox->bg_rgb = 0xffffff;
	g_object_set (G_OBJECT (textbox->scrolled_canvas), "background-color-rgb", textbox->bg_rgb, NULL);
	textbox->background = goo_canvas_image_new (textbox->scrolled_canvas_root, bg, 0, 0, NULL);
	return TRUE;
}

gboolean textbox_set_background_rgb(TextBox *textbox, int rgb) {
	if (!textbox)
		return FALSE;
	if (textbox->background) {
		goo_canvas_item_remove(textbox->background);
		textbox->background = NULL;
	}
	textbox->bg_rgb = rgb;
	g_object_set (G_OBJECT (textbox->scrolled_canvas), "background-color-rgb", textbox->bg_rgb, NULL);
	return TRUE;
}

void textbox_destroy(TextBox *textbox) {
	if (textbox->text)
		goo_canvas_item_remove(textbox->text);
	if (textbox->background)
		goo_canvas_item_remove(textbox->background);
	if (textbox->scrolled_canvas_root)
		goo_canvas_item_remove(textbox->scrolled_canvas_root);
	if (textbox->goo_scrolled_win)
		goo_canvas_item_remove(textbox->goo_scrolled_win);


	if (textbox->scrolled_win)
		g_object_unref(G_OBJECT (textbox->scrolled_win));
	if (textbox->scrolled_canvas)
		g_object_unref(G_OBJECT (textbox->scrolled_canvas));
	g_free(textbox);
}

gboolean textbox_translate(TextBox *textbox, Translate_type translate){
	printf("start func %s\n" ,__FUNCTION__);
	if (!textbox ||!textbox->vadjustment)
		return FALSE;

	gdouble cur_adjustment_value;
	cur_adjustment_value = gtk_adjustment_get_value(textbox->vadjustment);
	printf("befort set: %f\n", cur_adjustment_value);
	switch (translate)
	{
	case TRANSLATE_UP:
		cur_adjustment_value -=2.0;
		/*if (cur_adjustment_value <= textbox->vadjustment_lower) {
			cur_adjustment_value = textbox->vadjustment_lower;
		}*/
		printf("cur_adjustment_value: %f\n", cur_adjustment_value);
		gtk_adjustment_set_value(textbox->vadjustment, cur_adjustment_value);
		return FALSE;
		break;
	case TRANSLATE_DOWN:
		cur_adjustment_value += 2.0;
		/*if (cur_adjustment_value >= textbox->vadjustment_upper) {
			cur_adjustment_value = textbox->vadjustment_upper;
		}*/

		printf("cur_adjustment_value: %f\n", cur_adjustment_value);
		gtk_adjustment_set_value(textbox->vadjustment, cur_adjustment_value);
		return FALSE;
		break;
	default:
		break;
	}
}
